package com.facebank.usersupport.service.impl;

import com.facebank.usersupport.common.MessageKeyEnum;
import com.facebank.usersupport.mapper.usersupport.usersupport.AssMonthMapper;
import com.facebank.usersupport.mapper.usersupport.usersupport.AssociatorMapper;
import com.facebank.usersupport.model.AssMonth;
import com.facebank.usersupport.model.Associator;
import com.facebank.usersupport.model.RestModel;
import com.facebank.usersupport.service.IAssociatorService;
import com.facebank.usersupport.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.soap.SAAJMetaFactory;
import java.util.List;

@Service
public class AssociatorServiceImpl implements IAssociatorService {

    @Autowired
    private AssociatorMapper associatorMapper;
    @Autowired
    private AssMonthMapper assMonthMapper;
    /**
     * 获取会员列表
     * @param pageNo 页码
     * @param length 每页条数
     * @param associatorName 会员名称
     * @param phone 会员联系电话
     * @param status 会员账号状态
     * @param startTime 查询开始时间
     * @param endTime 查询结束时间
     * @return
     */
    @Override
    public RestModel list(Integer pageNo, Integer length, String associatorName, String phone, Short status, Long startTime, Long endTime) {
        status = (short) StrUtil.parseStringToInt(status,-1);
        PageHelper.startPage(pageNo,length);
        List<Associator> associators = associatorMapper.selectAssociatorList(associatorName, phone, status, startTime, endTime);
        PageInfo<Associator> pageInfo =new PageInfo<>(associators);
        return new RestModel(pageInfo);
    }

    /**
     * 新增客户，默认用户名为用户+手机号码字符串，默认密码为000000
     * @param phone 手机号码
     * @return
     */
    @Override
    public RestModel add(String phone) {
        if(StringUtils.isBlank(phone)&&phone.length()<5){
            return new RestModel(MessageKeyEnum.PARAM_ILLEGAL.getCode(),MessageKeyEnum.PARAM_ILLEGAL.getMessage());
        }
        if(associatorMapper.selectByAssociatorNameOrPhone(null,phone)==0){
            return new RestModel(MessageKeyEnum.ASSOCIATOR_PHONE_EXIST.getCode(),MessageKeyEnum.ASSOCIATOR_PHONE_EXIST.getMessage());
        }
        Associator associator = new Associator();
        associator.setPhone(phone);
        associator.setAssociatorName("用户"+phone);
        associator.setPassword("000000");
        Short status = 2;
        associator.setStatus(status);
        associatorMapper.insertSelective(associator);
        AssMonth assMonth =new AssMonth();
        return new RestModel("新增客户成功");
    }

    @Override
    public RestModel addByDetail(Associator associator) {
        associator.setPassword("123");
        associator.setCreateTime(System.currentTimeMillis());
        return new RestModel(associatorMapper.insertSelective(associator));
    }

    /**
     * 通过id删除客户
     * @param associatorId
     * @return
     */
    @Override
    public RestModel delete(Long associatorId) {
        if(associatorId==null){
            return new RestModel(MessageKeyEnum.PARAM_ILLEGAL.getCode(),MessageKeyEnum.PARAM_ILLEGAL.getMessage());
        }
        if(associatorMapper.selectByPrimaryKey(associatorId)==null){
            return new RestModel(MessageKeyEnum.USER_NOT_EXIST.getCode(),MessageKeyEnum.USER_NOT_EXIST.getMessage());
        }
        associatorMapper.deleteByPrimaryKey(associatorId);
        return new RestModel("删除客户成功");
    }

    /**
     * 通过Associator更新客户
     * @param associator
     * @return
     */
    @Override
    public RestModel update(Associator associator) {
        if(associatorMapper.selectByPrimaryKey(associator.getAssociatorId())==null){
            return new RestModel(MessageKeyEnum.USER_NOT_EXIST.getCode(),MessageKeyEnum.USER_NOT_EXIST.getMessage());
        }
        associator.setUpdateTime(System.currentTimeMillis());
        associator.setPassword("123");
        associatorMapper.updateByPrimaryKeySelective(associator);
        return new RestModel("修改客户成功");
    }
}
